# 注册实例页面,3个表单的注册操作
from selenium import webdriver
from selenium.webdriver.common.by import By
import os
import time
# TODO 导入select类
from selenium.webdriver.support.ui import Select


class Register:
    def __init__(self):
        # 创建对象 进入页面地址
        self.driver = webdriver.Chrome()
        url = 'file:///' + os.path.abspath('html') + '/注册实例.html'
        self.driver.get(url)


    def login(self):
        time.sleep(2)
        # 注册  定位用户名，输入信息
        user = self.driver.find_element(By.ID, 'user')
        user.send_keys('star')

        # 定位密码，输入
        time.sleep(2)
        pw = self.driver.find_element(By.ID, 'password')
        pw.send_keys('12345')

        # 定位电话号码，输入
        time.sleep(2)
        tel = self.driver.find_element(By.ID, 'tel')
        tel.send_keys('123')

        # 定位电子邮件，输入
        time.sleep(2)
        email = self.driver.find_element(By.ID, 'email')
        email.send_keys('123@qq.com')

        # 点击注册用户
        time.sleep(2)
        btn = self.driver.find_element(By.XPATH, '//*[@id="zc"]/fieldset/button')
        btn.click()

    def select(self):
        # 注册页面的select 下拉选项 运用select类的方法
        # 创建select对象，参数是要操作的select
        s1 = self.driver.find_element(By.ID, 'select')
        select = Select(s1)

        # TODO 选择option
        # 通过索引选取option，且索引从0开始
        select.select_by_index(2)

        # 通过value值选取option
        select.select_by_value('gz')

        # 通过文本选取option
        select.select_by_visible_text('重庆')

    def alt(self):
        # 定位alter键
        time.sleep(2)
        self.driver.find_element(By.ID, 'alert').click()

        # 切换到弹窗
        alter = self.driver.switch_to.alert
        time.sleep(2)
        alter.accept()  # 确定


    def frame(self):
        # TODO 切换到指定frame  切换至注册A
        time.sleep(2)
        frame1 = self.driver.switch_to.frame('idframe1')

        # 切换后，定位用户名a，输入

        userA = self.driver.find_element(By.ID, 'userA')
        userA.send_keys('haha')

        # 定位密码a，输入
        time.sleep(2)
        pwA = self.driver.find_element(By.ID, 'passwordA')
        pwA.send_keys('1234')

        # 定位电话a，输入
        time.sleep(2)
        telA = self.driver.find_element(By.ID, 'telA')
        telA.send_keys('123')

        # 定位邮箱a，输入
        time.sleep(2)
        emailA = self.driver.find_element(By.ID, 'emailA')
        emailA.send_keys('1122@qq.com')
        # 定位注册按钮并点击
        btnA = self.driver.find_element(By.XPATH, '//*[@id="zc"]/fieldset/button')
        btnA.click()

    def selectA(self):
        time.sleep(2)
        sA = self.driver.find_element(By.ID, 'selectA')
        selectA = Select(sA)
        selectA.select_by_value('gz')

    def alterA(self):
        # 定位alter键
        time.sleep(2)
        self.driver.find_element(By.ID, 'alerta').click()

        # 切换到弹窗
        alterA = self.driver.switch_to.alert
        time.sleep(2)
        alterA.accept()  # 确定




    def link_test(self):
        # TODO 定位微博，点击并切换到指定页面
        frame2 = self.driver.switch_to.frame('idframe1')
        self.driver.find_element(By.ID, 'fwA').click()

        news_handle = self.driver.window_handles[0]
        self.driver.switch_to.window(news_handle)

if __name__ == '__main__':
    resister = Register()

    resister.login()  # 注册
    resister.select()  # 下拉选项
    resister.alt()  # 弹窗

    resister.frame()  # 切换到指定的注册A

    resister.selectA()
    resister.alterA()
    # resister_a.link_test()
    # 不封装  关闭浏览器
    resister.driver.quit()

